/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.hs.srm.admin.modules.sys.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统页面视图
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月24日 下午11:05:27
 */
@Controller
public class SysPageController {

	@RequestMapping(value = {"/", "index.html"})
	public String index() {
		return "index";
	}

	@RequestMapping("login.html")
	public String login() {
		return "login";
	}

	@RequestMapping("main.html")
	public String main() {
		return "main";
	}

	@RequestMapping("modules/{module}/{url}.html")
	public ModelAndView module(@PathVariable("module") String module, @PathVariable("url") String url) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Enumeration<String> paramNames = request.getParameterNames();
		ModelAndView mav = new ModelAndView("modules/" + module + "/" + url);
		while(paramNames.hasMoreElements()){
            String paramName = paramNames.nextElement();
            mav.addObject(paramName, request.getParameter(paramName));
        }
//		if("sysdata".equals(module) && "sysDict_info".equals(url) && request.getParameter("type") != null) {
//			String typeText = EnumUtils.getTextByCode(DictType.class, Integer.valueOf(request.getParameter("type")));
//			mav.addObject("typeText", typeText);
//		}
//		if(url != null && url.endsWith("_info") && request.getParameter("act") != null) {
//			String act = request.getParameter("act");
//			mav.addObject("actType", "add".equals(act) ? "新增" : "编辑");
//		}
		return mav;
	}

	@RequestMapping("modules/{module}/{url}.html/{id}")
	public ModelAndView module(@PathVariable("module") String module, @PathVariable("url") String url,
			@PathVariable("id") Long id) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Enumeration<String> paramNames = request.getParameterNames();
		ModelAndView mav = new ModelAndView("modules/" + module + "/" + url);
		mav.addObject("id", id);
		while(paramNames.hasMoreElements()){
            String paramName = paramNames.nextElement();
            mav.addObject(paramName, request.getParameter(paramName));
        }
//		if("sysdata".equals(module) && "sysDict_info".equals(url) && request.getParameter("type") != null) {
//			String typeText = EnumUtils.getTextByCode(DictType.class, Integer.valueOf(request.getParameter("type")));
//			mav.addObject("typeText", typeText);
//		}
//		if(url != null && url.endsWith("_info") && request.getParameter("act") != null) {
//			String act = request.getParameter("act");
//			mav.addObject("actType", "add".equals(act) ? "新增" : "编辑");
//		}
		return mav;
	}

	@RequestMapping("404.html")
	public String notFound() {
		return "404";
	}

	@RequestMapping("common/{url}.html")
	public String subModule(@PathVariable("url") String url) {
		return "common/" + url;
	}

	@RequestMapping("modules/{module}/{submodule}/{url}.html")
	public String subModule(@PathVariable("module") String module, @PathVariable("submodule") String subModule,
			@PathVariable("url") String url) {
		return "modules/" + module + "/" + subModule + "/" + url;
	}

	@RequestMapping("modules/{module}/{submodule}/{url}.html/{id}")
	public ModelAndView subModule(@PathVariable("module") String module, @PathVariable("submodule") String subModule,
			@PathVariable("url") String url, @PathVariable("id") Long id) {
		ModelAndView mav = new ModelAndView("modules/" + module + "/" + subModule + "/" + url);
		mav.addObject("id", id);
		return mav;
	}

}
